﻿Option Explicit On
Option Strict On
Imports System.Web.Security
Partial Class Account_passwordrecovery
    Inherits System.Web.UI.Page
    Dim u As MembershipUser
    Dim users As MembershipUserCollection

    Protected Sub btnRequest_Click(sender As Object, e As System.EventArgs) Handles btnRequest.Click
        Dim sendemail As New SendEmailViaGoogle()
        Dim strMailFrom As String = ConfigurationManager.AppSettings("smtpmailfrom").ToString()
        Dim requestedemail As String = String.Empty
        Dim virtualurl As String = ("http://" + HttpContext.Current.Request.Url.Authority.ToString() + "/" + _
            HttpContext.Current.Request.Url.Segments(1).ToString() + "Account/LostPassword.aspx?id=").Replace("Account/Account/", "Account/")

        Dim finduserbyemail = Membership.FindUsersByEmail(txtUsernameOrEmail.Text).Cast(Of MembershipUser)().[Select](Function(x) x.UserName).FirstOrDefault()
        Dim finduserbyname = Membership.FindUsersByName(txtUsernameOrEmail.Text).Cast(Of MembershipUser)().[Select](Function(x) x.UserName).FirstOrDefault()

        If Not finduserbyemail Is Nothing Then
            u = Membership.GetUser(finduserbyemail)
            sendemail.SendEmail(strMailFrom, u.Email, "Lost Password Email.", virtualurl & u.ProviderUserKey.ToString())
            lblMessageSent.Text = "If these details are found an email will be sent. Please check your inbox."


        ElseIf Not finduserbyname Is Nothing Then
            u = Membership.GetUser(finduserbyname)
            sendemail.SendEmail(strMailFrom, u.Email, "Lost Password Email.", virtualurl & u.ProviderUserKey.ToString())
            lblMessageSent.Text = "If these details are found an email will be sent. Please check your inbox."
            lblMessageSent.ForeColor = Drawing.Color.Green

        Else
            lblMessageSent.Text = "If these details are found an email will be sent. Please check your inbox."
        End If

    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Request.QueryString("id") Is Nothing Then
            MultiView1.ActiveViewIndex = 0
        Else
            Dim qguid As Guid = New Guid(Request.QueryString("id"))
            u = Membership.GetUser(qguid)
            If u Is Nothing Then
                MultiView1.ActiveViewIndex = 0
            Else
                MultiView1.ActiveViewIndex = 1
                lblQuestio.Text = u.PasswordQuestion
            End If
        End If
    End Sub

    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim qguid As Guid = New Guid(Request.QueryString("id"))
        u = Membership.GetUser(qguid)
        lblNewPass.Text = u.ResetPassword(txtAnswer.Text)
    End Sub
End Class
